package com.yanqu.road.server.cmd.activity.dafuweng;

import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.enums.activity.dafuweng.eDaFuWengCardType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = GameProtocol.S_DA_FU_WENG_USE_CARD, desc = "使用卡片")
public class DaFuWengUseCardCmd extends AbstractCommand {

    private int cardId;
    private int count;
    private String param;

    List<Integer> eventIdList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MonopolyProto.UseCardReqMsg reqMsg = MonopolyProto.UseCardReqMsg.parseFrom(packet.getBytes());
        cardId = reqMsg.getCardId();
        param = reqMsg.getParam();
        count = reqMsg.getNum();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = DaFuWengMgr.checkGroup(player.getUserId());
        if (ret != 0) {
            return;
        }

        DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
        if (!ActivityMgr.activityInTime(dfwConfig.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_DFW_NO_IN_TIME;
            return;
        }
        eDaFuWengCardType type = dfwConfig.getCardConfigMap().get(cardId);
        if (type == null) {
            ret = GameErrorCode.E_ACTIVITY_DFW_PARAM_ERROR;
            return;
        }
        // 数量判断
        if (count <= 0) {
            ret = GameErrorCode.E_ACTIVITY_DFW_PARAM_ERROR;
            return;
        }
        if (type != eDaFuWengCardType.Gold &&
                type != eDaFuWengCardType.Silver &&
                type != eDaFuWengCardType.HuoWu &&
                type != eDaFuWengCardType.Energy &&
                type != eDaFuWengCardType.YongBing) {
            if (count > 1) {
                ret = GameErrorCode.E_ACTIVITY_DFW_PARAM_ERROR;
                return;
            }
        }

        DaFuWengMgr.useCard(player, cardId, count, param);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyProto.UseCardRespMsg.Builder respMsg = MonopolyProto.UseCardRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_DA_FU_WENG_USE_CARD, respMsg);
        }
    }
}
